﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Categories;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Media;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class BestSellersMini : BaseNopFrontendUserControl
    {
        protected override void OnInit(EventArgs e)
        {
            BindData();
            base.OnInit(e);
        }

        private void BindData()
        {
            if (this.SettingManager.GetSettingValueBoolean("Display.ShowBestsellersOnMainPage"))
            {
                int number = this.SettingManager.GetSettingValueInteger("Display.ShowBestsellersOnMainPageNumber");
                var report = this.OrderService.BestSellersReport(720, number, 1);
                if (report.Count > 0)
                {
                    List<Product> productList = new List<Product>();
                    foreach (BestSellersReportLine line in report)
                    {
                        var productVariant = this.ProductService.GetProductVariantById(line.ProductVariantId);
                        if (productVariant != null)
                        {
                            var product = productVariant.Product;
                            if (product != null)
                            {
                                bool contains = false;
                                foreach (Product p in productList)
                                {
                                    if (p.ProductId == product.ProductId)
                                    {
                                        contains = true;
                                        break;
                                    }
                                }
                                if (!contains)
                                {
                                    productList.Add(product);
                                }
                            }
                        }
                    }
                    dlCatalog.DataSource = productList;
                    dlCatalog.DataBind();
                }
                else
                {
                    this.Visible = false;
                }
            }
            else
            {
                this.Visible = false;
            }
        }

        protected void dlCatalog_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var product = e.Item.DataItem as Product;
                if (product != null)
                {
                    string productURL = SEOHelper.GetProductUrl(product);

                    var hlImageLink = e.Item.FindControl("hlImageLink") as HyperLink;
                    if (hlImageLink != null)
                    {
                        var picture = product.DefaultPicture;
                        if (picture != null)
                        {
                            hlImageLink.ImageUrl = this.PictureService.GetPictureUrl(picture, 100, true);
                        }
                        else
                        {
                            hlImageLink.ImageUrl = this.PictureService.GetDefaultPictureUrl(100);
                        }
                        hlImageLink.NavigateUrl = productURL;
                        hlImageLink.ToolTip = String.Format(GetLocaleResourceString("Media.Product.ImageLinkTitleFormat"), product.LocalizedName);
                        hlImageLink.Text = String.Format(GetLocaleResourceString("Media.Product.ImageAlternateTextFormat"), product.LocalizedName);
                    }

                    var hlProduct = e.Item.FindControl("hlProduct") as HyperLink;
                    if (hlProduct != null)
                    {
                        hlProduct.NavigateUrl = productURL;
                        hlProduct.Text = Server.HtmlEncode(product.LocalizedName);
                    }

                    var lbPrice = e.Item.FindControl("lbPrice") as Label;
                    if (lbPrice != null)
                    {
                        lbPrice.Text = GetPrice(product);
                    }
                }
            }
        }

        private string GetPrice(Product product)
        {
            var productVariantCollection = product.ProductVariants;
            if (productVariantCollection.Count > 0)
            {
                if (!product.HasMultipleVariants)
                {
                    var productVariant = productVariantCollection[0];
                    if (!this.SettingManager.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                        (NopContext.Current.User != null &&
                        !NopContext.Current.User.IsGuest))
                    {
                        if (productVariant.CustomerEntersPrice)
                        {
                            return string.Empty;
                        }
                        else
                        {
                            if (productVariant.CallForPrice)
                            {
                                return GetLocaleResourceString("Products.CallForPrice");
                            }
                            else
                            {
                                decimal taxRate = decimal.Zero;
                                decimal oldPriceBase = this.TaxService.GetPrice(productVariant, productVariant.OldPrice, out taxRate);
                                decimal finalPriceBase = this.TaxService.GetPrice(productVariant, PriceHelper.GetFinalPrice(productVariant, true), out taxRate);

                                decimal oldPrice = this.CurrencyService.ConvertCurrency(oldPriceBase, this.CurrencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);
                                decimal finalPrice = this.CurrencyService.ConvertCurrency(finalPriceBase, this.CurrencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);

                                if (finalPriceBase != oldPriceBase && oldPriceBase != decimal.Zero)
                                {
                                    return PriceHelper.FormatPrice(finalPrice);
                                }
                                else
                                {
                                    return PriceHelper.FormatPrice(finalPrice);
                                }
                            }
                        }
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                else
                {
                    var productVariant = product.MinimalPriceProductVariant;
                    if (productVariant != null)
                    {
                        if (!this.SettingManager.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                            (NopContext.Current.User != null &&
                            !NopContext.Current.User.IsGuest))
                        {
                            if (productVariant.CustomerEntersPrice)
                            {
                                return string.Empty;
                            }
                            else
                            {
                                if (productVariant.CallForPrice)
                                {
                                    return GetLocaleResourceString("Products.CallForPrice");
                                }
                                else
                                {
                                    decimal taxRate = decimal.Zero;
                                    decimal fromPriceBase = this.TaxService.GetPrice(productVariant, PriceHelper.GetFinalPrice(productVariant, false), out taxRate);
                                    decimal fromPrice = this.CurrencyService.ConvertCurrency(fromPriceBase, this.CurrencyService.PrimaryStoreCurrency, NopContext.Current.WorkingCurrency);

                                    return String.Format(GetLocaleResourceString("Products.PriceRangeFromText"), PriceHelper.FormatPrice(fromPrice));
                                }
                            }
                        }
                        else
                        {
                            return string.Empty;
                        }
                    }
                }
            }
            else
            {
                return string.Empty;
            }
            return string.Empty;
        }
    }
}